package com.atguigu.day01;

import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2022/6/28  17:25
 */
@Slf4j(topic = "c.CompletableFutureDemo02")
public class CompletableFutureDemo02 {
    public static void main(String[] args) {
        test3();
    }
    private static ExecutorService executorService = Executors.newFixedThreadPool(5, r -> {
        Thread thread = new Thread(r);

        thread.setName("高值区数据处理线程");

        return thread;
    });

    private static  void  test1(){
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> "Hello world")
                .thenApply(s -> s + " QQ")
                .thenApply(String::toUpperCase);

        System.out.println(stringCompletableFuture.join());
    }

    private static void testOr() {
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {

            TimeUtils.sleep(1);
            return String.valueOf("1");
        });
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            TimeUtils.sleep(2);
            return String.valueOf("2");
        });
        CompletableFuture<String> completableFuture = f1.applyToEither(f2, s -> s);

        System.out.println(completableFuture.join());
    }
    private static void test3(){

        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {

            TimeUtils.sleep(1);
            return String.valueOf("1");
        });
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            TimeUtils.sleep(2);
            return String.valueOf("2");
        });

        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(f1, f2);

        completableFuture.thenRun(()->{
         Integer X=   1/0;
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaa");
            test();
        });

        try {
            completableFuture.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    private static  void  test(){
        System.out.println("bbbbbbbbbbbbbbbbbbbbb");
    }
}
